/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.settlement.web.carinvoice;

import com.alibaba.fastjson.JSONValidator;
import com.jeesite.common.codec.EncodeUtils;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.base.entity.cars.Tcar;
import com.jeesite.modules.base.entity.customer.Customer;
import com.jeesite.modules.base.service.cars.TcarService;
import com.jeesite.modules.base.service.customer.CustomerService;
import com.jeesite.modules.custom.enumInfo.invoiceCategoryEnum;
import com.jeesite.modules.order.entity.serviceorder.ServiceOrder;
import com.jeesite.modules.order.service.serviceorder.ServiceOrderService;
import com.jeesite.modules.settlement.entity.carinvoice.CarInvoice;
import com.jeesite.modules.settlement.entity.customercarcontract.CustomerCarContract;
import com.jeesite.modules.settlement.entity.customercarcontract.CustomerCarContractDetail;
import com.jeesite.modules.settlement.service.carinvoice.CarInvoiceService;
import com.jeesite.modules.settlement.service.customercarcontract.CustomerCarContractService;
import com.jeesite.modules.sys.entity.EmpUser;
import com.jeesite.modules.sys.service.EmpUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 车辆发票Controller
 *
 * @author LV
 * @version 2021-03-09
 */
@Controller
@RequestMapping(value = "${adminPath}/settlement/carinvoice/carInvoice")
public class CarInvoiceController extends BaseController {

    @Autowired
    private CarInvoiceService carInvoiceService;
    @Autowired
    private CustomerCarContractService customerCarContractService;
    @Autowired
    private ServiceOrderService serviceOrderService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private EmpUserService empUserService;
    @Autowired
    private TcarService tcarService;

    /**
     * 获取数据
     */
    @ModelAttribute
    public CarInvoice get(String carInvoiceId, boolean isNewRecord) {
        return carInvoiceService.get(carInvoiceId, isNewRecord);
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("settlement:carinvoice:carInvoice:view")
    @RequestMapping(value = {"list", ""})
    public String list(CarInvoice carInvoice, Model model) {
        model.addAttribute("listServiceOrder", serviceOrderService.findList(new ServiceOrder()));// 服务订单号
        model.addAttribute("listCustomerCarCon", customerCarContractService.findList(new CustomerCarContract())); // 客户汽车供需合同
        model.addAttribute("listCustomer", customerService.findList(new Customer()));
        model.addAttribute("listUser", empUserService.findList(new EmpUser()));
        model.addAttribute("carInvoice", carInvoice);
        return "modules/settlement/carinvoice/carInvoiceList";
    }

    /**
     * 查询列表数据
     */
    @RequiresPermissions("settlement:carinvoice:carInvoice:view")
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page<CarInvoice> listData(CarInvoice carInvoice, HttpServletRequest request, HttpServletResponse response) {
        carInvoice.setPage(new Page<>(request, response));
        Page<CarInvoice> page = carInvoiceService.findPage(carInvoice);
        return page;
    }

    /**
     * 查看编辑表单
     */
    @RequiresPermissions("settlement:carinvoice:carInvoice:view")
    @RequestMapping(value = "form")
    public String form(CarInvoice carInvoice, Model model) {
        model.addAttribute("listServiceOrder", serviceOrderService.findList(new ServiceOrder()));// 服务订单号
        model.addAttribute("listCustomerCarCon", customerCarContractService.findList(new CustomerCarContract())); // 客户汽车供需合同
        model.addAttribute("listCustomer", customerService.findList(new Customer()));
        model.addAttribute("tcarList", tcarService.findList(new Tcar()));
        carInvoice.setInvoiceItem(invoiceCategoryEnum.CAR_INVOICE.getCode());
        model.addAttribute("carInvoice", carInvoice);
        return "modules/settlement/carinvoice/carInvoiceForm";
    }

    /**
     * 查看表单详情
     */
    @RequiresPermissions("settlement:carinvoice:carInvoice:view")
    @RequestMapping(value = "detail")
    public String detail(CarInvoice carInvoice, Model model) {
        model.addAttribute("listServiceOrder", serviceOrderService.findList(new ServiceOrder()));// 服务订单号
        model.addAttribute("listCustomerCarCon", customerCarContractService.findList(new CustomerCarContract())); // 客户汽车供需合同
        model.addAttribute("listCustomer", customerService.findList(new Customer()));
        model.addAttribute("tcarList", tcarService.findList(new Tcar()));
        model.addAttribute("carInvoice", carInvoice);
        return "modules/settlement/carinvoice/carInvoiceDetail";
    }

    /**
     * 保存数据
     */
    @RequiresPermissions("settlement:carinvoice:carInvoice:edit")
    @PostMapping(value = "save")
    @ResponseBody
    public String save(@Validated CarInvoice carInvoice) {
        carInvoiceService.save(carInvoice);
        return renderResult(Global.TRUE, text("保存车辆发票成功！"));
    }

    /**
     * 删除数据
     */
    @RequiresPermissions("settlement:carinvoice:carInvoice:delete")
    @RequestMapping(value = "delete")
    @ResponseBody
    public String delete(CarInvoice carInvoice) {
        carInvoiceService.delete(carInvoice);
        return renderResult(Global.TRUE, text("删除车辆发票成功！"));
    }

    /**
     * 列表选择对话框
     */
    @RequiresPermissions("settlement:carinvoice:carInvoice:view")
    @RequestMapping(value = "carInvoiceSelect")
    public String empUserSelect(CarInvoice carInvoice, String selectData, Model model) {
        String selectDataJson = EncodeUtils.decodeUrl(selectData);
        if (selectDataJson != null && JSONValidator.from(selectDataJson).validate()) {
            model.addAttribute("selectData", selectDataJson);
        }
        model.addAttribute("CarInvoice", carInvoice);
        return "modules/settlement/carinvoice/carInvoiceSelect";
    }

    @RequiresPermissions("settlement:carinvoice:carInvoice:view")
    @RequestMapping(value = "createFromCusCarCont")
    public String createFromCusCarCont(CustomerCarContract customerCarContract, String rowId, Model model) {
        CustomerCarContract oldCustomerCarContract = customerCarContractService.get(customerCarContract.getCustomerCarContractId());
        CarInvoice carInvoice = new CarInvoice();
        // 服务订单
        ServiceOrder so = new ServiceOrder();
        so.setServiceOrderId(oldCustomerCarContract.getServiceOrder().getServiceOrderId());
        carInvoice.setServiceOrder(so);

        // 来源自
        carInvoice.setCustomerCarCon(customerCarContract);

        // 客户
        Customer customer = new Customer();
        customer.setCustomerId(oldCustomerCarContract.getCustomer().getCustomerId());
        carInvoice.setCustomer(customer);

        // 车架号 + 车辆名称
        List<CustomerCarContractDetail> customerCarContractDetailList = oldCustomerCarContract.getCustomerCarContractDetailList();
        for (CustomerCarContractDetail customerCarContractDetail : customerCarContractDetailList) {
            if (rowId.equals(customerCarContractDetail.getCustomerCarContractDetailId())) {
                carInvoice.setFrameNumber(customerCarContractDetail.getFrameNumber());
                Tcar tcar = new Tcar();
                tcar.setCarId(customerCarContractDetail.getCarId());
                carInvoice.setTcar(tcar);
            }
        }


        model.addAttribute("listServiceOrder", serviceOrderService.findList(new ServiceOrder()));// 服务订单号
        model.addAttribute("listCustomerCarCon", customerCarContractService.findList(new CustomerCarContract())); // 客户汽车供需合同
        model.addAttribute("listCustomer", customerService.findList(new Customer()));
        model.addAttribute("tcarList", tcarService.findList(new Tcar()));

        carInvoice.setInvoiceItem(invoiceCategoryEnum.CAR_INVOICE.getCode());
        model.addAttribute("carInvoice", carInvoice);
        return "modules/settlement/carinvoice/carInvoiceForm";
    }


}